use axum::Json;
use serde_json::{Value, json};
use crate::{reqwest, encrypt, query };
// 微信登录
pub async fn login(code:&str, appid:&str, secret:&str ) -> Result<Value, Json<Value>>{
    let grant_type = "authorization_code";
    let url = format!(
        "https://api.weixin.qq.com/sns/jscode2session?appid={}&secret={}&js_code={}&grant_type={}",
        appid, secret, code, grant_type
    );
    let rs = reqwest::get(&url).await?;
    
    let ident = query::str(&rs, "openid")?;
    let ident = encrypt::magic_crypt::encode_64(&ident);

    let veri = query::str(&rs, "session_key")?;
    let veri = encrypt::magic_crypt::encode_64(&veri);

    let casc = query::str_op(&rs, "unionid")?;

    Ok( json!({ "ident": ident, "casc": casc, "veri": veri }) )
}
// 检验登录态
/*
pub async fn _check_session_key(ident:&str, signature:&str, secret:&str ) -> Result<Json<Value>, Json<Value>>{
    let sig_method = "hmac_sha256";
    let access_token = get_access_token(ident, secret).await?;
    let url = format!(
        "https://api.weixin.qq.com/wxa/checksession?access_token={}&ident={}&signature={}&sig_method={}",
        access_token, ident, signature, sig_method
    );
    let rs = reqwest::get(&url).await?;
    let ident = rs.get("ident");
    
    if ident.is_some() {
        Ok( code::data(rs) )
    }else{
        Err( code::data(rs) )
    }
}
 */